<?php
require_once("model/news.class.php");
include_once("controller/JKUpload.class.php");
include_once("includes/util.php");



/**
 * manage class. Process for all function in manage tab of admin page
 */
class Bnews
{
	var $db;
 	var $smarty;
 	
    function Bnews($db, $smarty)
	{
		//store pointer to db onject
		$this->db = $db;
		$this->smarty = $smarty;
	}
	
	/**
	 * Process Ajax call for add new category.
	 */
	function addNews()
	{
		$msg = news::validateNews();	
		$arr = array("code" => -1, "message" => _("Could not add news."));
		if($msg == ""){
			if($_FILES['news_image']['name'] != NULL && $_FILES['news_image']['name'] != "") {
				$upload = new JKUpload();
				$image = $_FILES['news_image']['name'];
				$path = pathinfo($image);
				$image_type = strtolower($path['extension']);
				$namefile = explode(".".$image_type,$_FILES['news_image']['name']);
				if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
					$imagename = $upload->upload_news_thumbnail($_FILES['news_image']['tmp_name'], $namefile['0'], $image_type);
				} 
			}
			$news = array (
					'news_title' => $_POST['news_title'],
					'news_image'=>$imagename,
					'news_short'=>$_POST['news_short'],
					'tags'=>$_POST['tags'],
					'news_content'=>$_POST['news_content']
					);
			$id = news::saveNews($this->db,$news, $_SESSION['LC_ALL']);
			if($id >=0 ){
				unset($_SESSION['news']);
				$arr = array("code" => 0, "message" => _("News has been added."),
							"ID" => $id);
				//Merge POST information to return back client and show at client side
				$arr = array_merge($arr, $_POST);
			}
			setcookie("message", $msg,time()+5);
			header("Location: ./?mod=manage&sub1=news") ;
		}else{
			setcookie("message", $msg,time()+5);
			header("Location: ./?mod=manage&sub1=news&act=addnews") ;	
		}
		
		return;
	}
	
	/**
	 * Process Ajax call for delete a category
	 */
	function deleteNews()
	{
		$arr = array("code" => -1, "message" => _("Invalid Arguments."));
		if(isset( $_POST['id'])){
			$res = news::deleteNews($this->db, $_POST['id']);
			if($res)
			{
				$arr['code'] = 0;
				$arr['message'] = sprintf(_("News with Id %s has been deleted."),
										"<b>". $_POST['id'] . "</b>");
				@unlink("../".UPLOAD_NEWS . $_POST['news_image']);
				@unlink("../".UPLOAD_NEWS_THUMB . $_POST['news_image']);
			}
		}
		echo array2json($arr);
		
		return;	//we use ajax delete product, so no need to show rest of page here
	}
	
	/**
	 * Process Ajax call for update a category
	 */
	function editNews(){
		if(isset( $_POST['news_id'])){
			$msgId = 2;
			$msg = news::validateNews();
			if($msg == "")
			{
				$news = news::getNews($this->db,$_POST['news_id']);
				if($_FILES['news_image']['name'] != NULL && $_FILES['news_image']['name'] != "") {
					$upload = new JKUpload();
					$image = $_FILES['news_image']['name'];
					$path = pathinfo($image);
					$image_type = strtolower($path['extension']);
					$namefile = explode(".".$image_type,$_FILES['news_image']['name']);
					if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
						$imagename = $upload->upload_news_thumbnail($_FILES['news_image']['tmp_name'], $namefile['0'], $image_type);
					} 
					@unlink("../".UPLOAD_NEWS.$news->news_image);
					@unlink("../".UPLOAD_NEWS_THUMB.$news->news_image);
				}else{
					$imagename = $news->news_image;
				}
				$news = array (
					'news_id' => $_POST['news_id'],
					'news_title' => $_POST['news_title'],
					'news_image'=>$imagename,
					'news_short'=>$_POST['news_short'],
					'tags'=>$_POST['tags'],
					'news_content'=>$_POST['news_content']
					);
				$res = news::updateNews($this->db, $news, $_SESSION['LC_ALL']);

				$msgId = 1;
				//setcookie("message", $msg,time()+5);
				//header("Location: ./?mod=manage&sub1=news") ;	
			}else{
				//setcookie("message", $msg,time()+5);
				//header("Location: ./?mod=manage&sub1=news&act=editnews") ;	
			}
		}	
	}
	
	
}
?>